<template>
    <el-dialog class="ba-operate-dialog" :close-on-click-modal="false" v-model="visible">
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">上传文件</div>
        </template>
        <el-scrollbar v-loading="loading" class="ba-table-form-scrollbar">
            <div class="ba-operate-form" :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'">
                <el-form :model="row" label-width="120px">
                    <el-form-item label="文件分类" prop="category">
                        <el-select v-model="row.category" placeholder="请选择文件分类">
                            <el-option v-for="item in categoryList" :key="item.value" :label="item.label" :value="item.value" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="文件" prop="files">
                        <baUpload
                            type="files"
                            v-model="row.files"
                            hideSelectFile
                            :show-file-list="true"
                            list-type="picture-card"
                            :on-success="onSuccess"
                            :data="{ category: row.category }" />
                    </el-form-item>
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="onReset">重置</el-button>
            </div>
        </template>
    </el-dialog>
</template>
<script lang="ts" setup>
import { inject, ref, defineExpose } from 'vue'
import type baTableClass from '/@/utils/baTable'
import { useConfig } from '/@/stores/config'
import baUpload from '/@/components/baInput/components/baUpload.vue'

const visible = ref(false)
const loading = ref(false)
const config = useConfig()
const baTable = inject('baTable') as baTableClass

const row = ref({
    category: '',
    files: [],
})

const categoryList = ref([])
// 取消
const onReset = () => {
    row.value = {
        category: '',
        files: [],
    }
}
// 打开弹窗
const onOpen = () => {
    onReset()
    visible.value = true
}

const onSuccess = () => {
    baTable.onTableAction('com-search', {})
}
defineExpose({
    categoryList,
    onOpen,
})
</script>
